<template>
  <div style="text-align: center">
    <el-form :inline="true" :model="dataForm" ref="dataForm" @keyup.enter.native="getDataList()" size="mini" label-width="150px">
      <el-form-item label="规则集合编号：">
        <el-input v-model="dataForm.droolsNum" placeholder="droolsNum" clearable></el-input>
      </el-form-item>
      <el-form-item label="规则集合名称：">
        <el-input v-model="dataForm.droolsName" placeholder="jarName" clearable></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button @click="getDataList()" size="mini" type="primary">查询</el-button>
      <el-button @click="reset()" size="mini">重置</el-button>
    </div>
    <hr/>
    <div style="margin-bottom: 5px">
      <el-button v-if="isAuth('project:rule:add')"  type="primary" size="mini" @click="addOrUpdateHandle(null)">新增</el-button>
      <el-button v-if="isAuth('project:rule:delete')" type="danger"  size="mini" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      <el-button v-if="isAuth('project:rule:platform')" type="primary" size="mini" @click="openRulePlatform">规则引擎平台</el-button>
    </div>

    <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;border-radius: 5px" size="mini" class="width-bg"
              border tooltip-effect="dark" :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="droolsNum" header-align="center" align="center" label="规则集合编号">
      </el-table-column>
      <el-table-column prop="droolsName" header-align="center" align="center" label="规则集合名称">
      </el-table-column>
      <el-table-column prop="userName" header-align="center" align="center" label="用户名">
      </el-table-column>
      <el-table-column prop="password" header-align="center" align="center" label="密码">
      </el-table-column>
      <el-table-column prop="url" header-align="center" align="center" label="链接前缀">
      </el-table-column>
      <el-table-column prop="basicAuthentication" header-align="center" align="center" label="认证类型">
      </el-table-column>
      <el-table-column prop="packageName" header-align="center" align="center" label="包名">
      </el-table-column>
      <el-table-column prop="version" header-align="center" align="center" label="版本号">
      </el-table-column>
      <el-table-column prop="jarName" header-align="center" align="center" label="jar包名">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('project:dictionary:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('project:dictionary:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
    import AddOrUpdate from './droolsBaseInfo-add-or-update'
    import { droolsBaseInfoPage , droolsBaseInfoDelete } from '@/api/droolsBaseInfo'
    export default {
        data () {
            return {
                dataForm: {
                    id:null,
                    droolsNum:null,
                    droolsName:null,
                    userName:null,
                    password:null,
                    url:null,
                    basicAuthentication:null,
                    packageName:null,
                    version:null,
                    jarName:null,
                    createTime:null,
                    updateTime:null,
                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false
            }
        },
        components: {
            AddOrUpdate
        },
        activated () {
            this.getDataList()
        },
        methods: {
            // 获取数据列表
            getDataList () {
                this.dataListLoading = true
                const param= {
                    'current': this.pageIndex,
                    'size': this.pageSize,
                    'param': this.dataForm
                }
                const requestData={
                    'requestData':param
                }
                droolsBaseInfoPage(requestData).then((res)=>{
                    this.dataListLoading = false
                    if(res.code=='0000'){
                        this.dataList = res.responseData.records
                        this.totalPage = res.responseData.total
                    }else{
                        this.dataList = []
                        this.totalPage = 0
                    }
                }).catch((err)=>{

                })
            },
            // 每页数
            sizeChangeHandle (val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle (val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 多选
            selectionChangeHandle (val) {
                this.dataListSelections = val
            },
            // 新增 / 修改
            addOrUpdateHandle (id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle (id) {
                var userIds = id ? [id] : this.dataListSelections.map(item => {
                    return item.id
                })
                this.$confirm("确定删除", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const requestData={
                        'requestData':userIds
                    }
                    droolsBaseInfoDelete(requestData).then((res)=>{
                        if(res.code=='0000'){
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getDataList()
                                }
                            })
                        }
                    }).catch((err)=>{
                        this.$message.error(err)
                    })
                }).catch(() => {})
            },
            openRulePlatform(){
                window.location.href="http://118.190.62.94:8088/kie-drools-wb/kie-drools-wb.jsp"
            },
            reset(){
                this.dataForm.id=null
                this.dataForm.droolsNum=null
                this.dataForm.userName=null
                this.dataForm.password=null
                this.dataForm.url=null
                this.dataForm.basicAuthentication=null
                this.dataForm.packageName=null
                this.dataForm.version=null
                this.dataForm.jarName=null
                this.dataForm.createTime=null
                this.dataForm.updateTime=null
                this.getDataList()
            }
        }
    }
</script>
